package demo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ArrayBlockingQueueDeadlockDemo {
    public static void main(String[] args) {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);

        Thread thread1 = new Thread(() -> {
            try {
                // 获取锁并插入元素
                queue.put("item");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.start();

        Thread thread2 = new Thread(() -> {
            try {
                // 获取锁并尝试插入元素，但由于队列已满而被阻塞
                queue.put("item");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread2.start();

        // 等待两个线程完成
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Finished.");
    }
}

